<template>
  <div class="contribution-grid">
    <div class="grid-container">
      <div class="legend">
        <span class="legend-text">{{ $t('events.lessActivity') }}</span>
        <div class="legend-item">
          <div class="legend-color" style="background-color: #161b22;"></div>
          <span>0</span>
        </div>
        <div class="legend-item">
          <div class="legend-color" style="background-color: #0e4429;"></div>
          <span>1-2</span>
        </div>
        <div class="legend-item">
          <div class="legend-color" style="background-color: #006d32;"></div>
          <span>3-4</span>
        </div>
        <div class="legend-item">
          <div class="legend-color" style="background-color: #26a641;"></div>
          <span>5-6</span>
        </div>
        <div class="legend-item">
          <div class="legend-color" style="background-color: #39d353;"></div>
          <span>7+</span>
        </div>
        <span class="legend-text">{{ $t('events.moreActivity') }}</span>
      </div>
      
      <div class="months">
        <div v-for="month in months" :key="month" class="month">{{ month }}</div>
      </div>
      
      <div class="grid">
        <div class="days">
          <div v-for="day in days" :key="day" class="day">{{ day }}</div>
        </div>
        
        <div class="weeks" ref="weeksContainer">
          <!-- Weeks and cells will be generated by JavaScript -->
        </div>
      </div>
    </div>
    
    <div class="stats">
      <div class="stat-card">
        <div class="stat-value">{{ totalContributions }}</div>
        <div class="stat-label">{{ $t('events.totalContributions') }}</div>
      </div>
      <div class="stat-card">
        <div class="stat-value">{{ streakDays }}</div>
        <div class="stat-label">{{ $t('events.streakDays') }}</div>
      </div>
      <div class="stat-card">
        <div class="stat-value">{{ activityRate }}%</div>
        <div class="stat-label">{{ $t('events.activityRate') }}</div>
      </div>
    </div>
    
    <div class="tooltip" ref="tooltip"></div>
  </div>
</template>

<script>
export default {
  name: 'ContributionGrid',
  data() {
    return {
      months: ['一月', '二月', '三月', '四月', '五月', '六月', '七月', '八月', '九月', '十月', '十一月', '十二月'],
      days: ['日', '一', '二', '三', '四', '五', '六'],
      colorLevels: [
        { min: 0, max: 0, color: '#161b22' },
        { min: 1, max: 2, color: '#0e4429' },
        { min: 3, max: 4, color: '#006d32' },
        { min: 5, max: 6, color: '#26a641' },
        { min: 7, max: Infinity, color: '#39d353' }
      ],
      totalContributions: 0,
      streakDays: 0,
      activityRate: 0
    }
  },
  mounted() {
    this.initializeGrid()
  },
  methods: {
    generateContributions() {
      const data = []
      const today = new Date()
      const startDate = new Date()
      startDate.setDate(today.getDate() - 364)
      
      for (let i = 0; i < 365; i++) {
        const date = new Date(startDate)
        date.setDate(startDate.getDate() + i)
        
        let count = Math.floor(Math.random() * 10)
        
        if (Math.random() > 0.8) count += Math.floor(Math.random() * 5)
        if (Math.random() > 0.9) count += Math.floor(Math.random() * 8)
        
        if (date.getDay() === 0 || date.getDay() === 6) {
          count = Math.max(0, count - Math.floor(Math.random() * 3))
        }
        
        data.push({
          date: date.toISOString().split('T')[0],
          count: count
        })
      }
      
      return data
    },
    
    getColor(count) {
      for (const level of this.colorLevels) {
        if (count >= level.min && count <= level.max) {
          return level.color
        }
      }
      return this.colorLevels[0].color
    },
    
    calculateStats(data) {
      this.totalContributions = data.reduce((sum, day) => sum + day.count, 0)
      
      let currentStreak = 0
      let maxStreak = 0
      let activeDays = 0
      
      data.forEach(day => {
        if (day.count > 0) {
          currentStreak++
          activeDays++
          maxStreak = Math.max(maxStreak, currentStreak)
        } else {
          currentStreak = 0
        }
      })
      
      this.streakDays = maxStreak
      this.activityRate = Math.round((activeDays / data.length) * 100)
    },
    
    renderGrid(data) {
      const weeksContainer = this.$refs.weeksContainer
      const tooltip = this.$refs.tooltip
      weeksContainer.innerHTML = ''
      
      const weeks = []
      for (let i = 0; i < data.length; i += 7) {
        weeks.push(data.slice(i, i + 7))
      }
      
      weeks.forEach(week => {
        const weekElement = document.createElement('div')
        weekElement.className = 'week'
        
        week.forEach(day => {
          const cell = document.createElement('div')
          cell.className = 'cell'
          cell.style.backgroundColor = this.getColor(day.count)
          
          cell.addEventListener('mouseenter', (e) => {
            const rect = cell.getBoundingClientRect()
            tooltip.style.left = (rect.left + window.scrollX) + 'px'
            tooltip.style.top = (rect.top + window.scrollY - 70) + 'px'
            
            tooltip.innerHTML = `
              <div class="count">${day.count} ${this.$t('events.contributions')}</div>
              <div class="date">${day.date}</div>
            `
            
            tooltip.style.opacity = 1
          })
          
          cell.addEventListener('mouseleave', () => {
            tooltip.style.opacity = 0
          })
          
          weekElement.appendChild(cell)
        })
        
        weeksContainer.appendChild(weekElement)
      })
    },
    
    initializeGrid() {
      const contributions = this.generateContributions()
      this.calculateStats(contributions)
      this.renderGrid(contributions)
    }
  }
}
</script>

<style scoped>
.contribution-grid {
  width: 100%;
  padding: 2rem;
}

.grid-container {
  background: rgba(13, 17, 23, 0.7);
  border: 1px solid #30363d;
  border-radius: 12px;
  padding: 2.5rem;
  backdrop-filter: blur(10px);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
  margin-bottom: 3rem;
  position: relative;
  overflow: hidden;
}

.grid-container::before {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle, rgba(88,166,255,0.1) 0%, rgba(13,17,23,0) 70%);
  z-index: -1;
  animation: rotate 30s linear infinite;
}

@keyframes rotate {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.legend {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  margin-bottom: 1.5rem;
  flex-wrap: wrap;
}

.legend-text {
  font-size: 0.9rem;
  color: #8b949e;
  margin-right: 10px;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 4px;
}

.legend-color {
  width: 12px;
  height: 12px;
  border-radius: 2px;
}

.months {
  display: flex;
  margin-left: 30px;
  margin-bottom: 10px;
}

.month {
  font-size: 0.85rem;
  color: #8b949e;
  width: calc(100% / 12);
  text-align: center;
}

.grid {
  display: flex;
  gap: 4px;
}

.days {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-right: 8px;
}

.day {
  font-size: 0.8rem;
  color: #8b949e;
  height: 15px;
  line-height: 15px;
  text-align: center;
  visibility: hidden;
}

.day:nth-child(2) { visibility: visible; }
.day:nth-child(4) { visibility: visible; }
.day:nth-child(6) { visibility: visible; }

.weeks {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  height: 105px;
  gap: 4px;
}

.week {
  display: flex;
  gap: 4px;
}

.cell {
  width: 15px;
  height: 15px;
  border-radius: 3px;
  background-color: #161b22;
  position: relative;
  cursor: pointer;
  transition: transform 0.2s ease, opacity 0.2s ease;
}

.cell:hover {
  transform: scale(1.3);
  opacity: 0.9;
  border: 1px solid rgba(255, 255, 255, 0.3);
  z-index: 10;
}

.tooltip {
  position: absolute;
  background: #0d1117;
  border: 1px solid #30363d;
  border-radius: 6px;
  padding: 0.8rem 1rem;
  color: #c9d1d9;
  font-size: 0.9rem;
  min-width: 160px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
  z-index: 100;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.3s ease;
  text-align: center;
}

.tooltip .count {
  font-weight: bold;
  font-size: 1.2rem;
  color: #58a6ff;
  margin-bottom: 0.3rem;
}

.tooltip .date {
  font-size: 0.85rem;
  color: #8b949e;
}

.stats {
  display: flex;
  justify-content: space-around;
  margin-top: 3rem;
  flex-wrap: wrap;
  gap: 2rem;
}

.stat-card {
  background: rgba(13, 17, 23, 0.6);
  border: 1px solid #30363d;
  border-radius: 10px;
  padding: 1.8rem;
  text-align: center;
  min-width: 220px;
  transition: transform 0.3s ease;
}

.stat-card:hover {
  transform: translateY(-8px);
  border-color: #58a6ff;
}

.stat-value {
  font-size: 2.5rem;
  font-weight: bold;
  color: #58a6ff;
  margin-bottom: 0.5rem;
}

.stat-label {
  color: #8b949e;
  font-size: 1.1rem;
}

@media (max-width: 768px) {
  .grid-container {
    padding: 1.5rem;
  }
  
  .months {
    margin-left: 20px;
  }
  
  .month {
    font-size: 0.7rem;
  }
  
  .stats {
    flex-direction: column;
    align-items: center;
  }
}
</style> 